package com.heking.zplayer.qlyopen;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import com.cmcc.qlyopensdk.init.IGlobalCache;
import com.cmcc.qlyopensdk.init.impl.GlobalCache;
import com.cmcc.qlyopensdk.init.impl.QlyOpenSdk;
import com.cmcc.qlyopensdk.log.QlyLogger;
import com.cmcc.qlyopensdk.net.bean.BaseResponse;
import com.cmcc.qlyopensdk.net.callback.NetCallBack;
import com.cmcc.qlyopensdk.net.restapi.IQlyOpenSdkApi;
import com.cmcc.qlyopensdk.net.restapi.impl.QlyOpenSdkApi;
import com.cmcc.qlyopensdk.player.constant.ServerEnv;
import com.cmcc.qlyopensdk.utils.DeviceUtils;
import com.cmcc.qlyopensdk.vo.response.QlyPlatformToken;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Method;

import kotlin.jvm.internal.Intrinsics;

/**
 * @Author: zhoug
 * @Date: 2024-11-05
 * @Description:
 */
public class ZQlyOpenSdk {
    private static final String TAG = ">>>ZQlyOpenSdk";
    /**
     * 获取token类型
     * 1：应用token(拥有该应用全部权限)，2：自定义token(拥有该应用部分权限)
     */
    public static final int TOKEN_OPERATOR_TYPE_APP = 1;
    public static final int TOKEN_OPERATOR_TYPE_CUSTOM = 2;
    /**
     * 资源类型
     * 2：设备，获取应用token时不传
     */
    public static final String TARGET_TYPE_DEVICE = "2";

    /**
     * 行为
     * read：只读，write：可读可写
     */
    public static final String ACTION_WRITE = "write";
    public static final String ACTION_READ = "read";

    public static final int QUALITY_HD = 1;//高清
    public static final int QUALITY_SD = 0;//标清

    /**
     * 初始化播放器
     *
     * @param application
     * @param debug
     */
    public static void init(Application application, String appId, String privateKey, boolean debug) {
        Log.d(TAG, "init:初始化 QlyOpenSdk");
        //设置为发布环境
        QlyOpenSdk.getInstance().setServerEnv(ServerEnv.SERVER_ENV_PRO);
        //打印日志
        QlyOpenSdk.getInstance().setDebug(debug);
        QlyOpenSdk.getInstance().init(application, appId, privateKey);
        /*QlyOpenSdk.getInstance().setLogCallback(new LogCallback() {
            @Override
            public void onPrintLog(String value1, String value2) {
                Log.d(TAG, "onPrintLog:value1="+value1+",value2="+value2);
            }
        });*/

    }

    public static void init(Application application) {
        init(application, QlyAccount.appId, QlyAccount.privateKey, BuildConfig.DEBUG);
    }

    /**
     * 设置token
     * 千里眼api没有回掉接口
     * @param token
     * @deprecated use {@link #setOpenPlatformToken1}
     */
    @Deprecated
    public static void setOpenPlatformToken(String token) {
        Log.d(TAG, "设置token:" + token);
        QlyOpenSdk.getInstance().setOpenPlatformToken(token);
    }

    /**
     * 设置token
     * 根据千里眼api源码重写,新增回掉接口
     *
     * @param token
     * @param callback
     */
    public static void setOpenPlatformToken1(String token, Callback2<Boolean> callback) {
        Log.d(TAG, "设置token:" + token);
        QlyOpenSdk.getInstance().setOpenPlatformToken(token);
        Intrinsics.checkNotNullParameter(token, "token");
        GlobalCache.getInstance().setConfig("open_platform_token", token);
        IQlyOpenSdkApi api = QlyOpenSdkApi.Companion.getInstance();
        api.getQlyPlatformToken("eab463a0ab013715704d805ddfa03471", token, new NetCallBack<BaseResponse<QlyPlatformToken>>() {
            @Override
            public void onResponse(BaseResponse<QlyPlatformToken> response) {
                if (response == null) {
                    if (callback != null) {
                        callback.onError(0,"response is null");
                    }
                    return;
                }
                if (response.isSuccess() && response.getData() != null) {
                    QlyPlatformToken qlyPlatformToken = response.getData();
                    if (!TextUtils.isEmpty(qlyPlatformToken.getToken()) && !TextUtils.isEmpty(qlyPlatformToken.getRegion())) {
                        IGlobalCache api = GlobalCache.getInstance();
                        String token = qlyPlatformToken.getToken();
                        api.setConfig("qly_platform_token", token);
                        api = GlobalCache.getInstance();
                        String region = qlyPlatformToken.getRegion();
                        api.setConfig("qly_platform_region", region);
                        if (callback != null) {
                            callback.onSuccess(true);
                        }
                    } else {
                        if (callback != null) {
                            callback.onError(0,"无效的千里眼Token");
                        }
                    }
                } else {
                    if (callback != null) {
                        int status=0;
                        String resultCode = response.getResultCode();
                        if("11504".equals(resultCode)){
                            status=PlayError.TOKEN_EXPIRE;
                        }
                        callback.onError(status,response.getResultMsg());
                    }
                }
            }

            @Override
            public void onError(Throwable throwable) {
                if (callback != null) {
                    callback.onError(0,throwable != null ? throwable.getMessage() : "设置token失败");
                }
            }
        });


    }

    public static String getMacByDeviceId(String deviceId) {
        return DeviceUtils.getMacByDeviceId(deviceId);
    }


}
